package com.jw.xzg.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.jw.xzg.R;
import com.jw.xzg.activity.LoginActivity;
import com.jw.xzg.activity.MainActivity;

/**
 * Created by Administrator on 2017/4/18.
 */

public class ThumbnailView extends ImageView {

    public ThumbnailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initClick();
    }

    public ThumbnailView(Context context, AttributeSet attrs,
                       int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        initClick();
    }
    public ThumbnailView(Context context) {
        super(context);
        initClick();
        // TODO Auto-generated constructor stub
    }


    public void initClick(){
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.btn_login://登录
                        LoginActivity._this.login(v);
                        break;
                    case R.id.btn_password://找回密码
                        LoginActivity._this.opeonForgetPasswordDialog(v);
                        break;
                    case R.id.btn_jump_to_login://返回登录
                        LoginActivity._this.jumpToLoginDialog(v);
                        break;
                    case R.id.btn_submit_password://确认修改
                        LoginActivity._this.forgetPasswordSubmit(v);
                        break;
                    case R.id.btn_change_password: //修改密码
                        MainActivity._this.onChangePasswordClick(v);
                        break;
                    case R.id.btn_logout://退出登录
                        MainActivity._this.onLogoutClick(v);
                        break;
                    case R.id.btn_change_password_submit://修改密码提交
                        MainActivity._this.onChangePasswordSubmitClick(v);
                        break;
                    case R.id.btn_bulid_junior: //初级建筑
                        MainActivity._this.onBuildJuniorClick(v);
                        break;
                    case R.id.btn_bulid_senior://中级建筑
                        MainActivity._this.onBuildSeniorClick(v);
                        break;
                    case R.id.btn_bulid_high://高级建筑
                        MainActivity._this.onBuildHighClick(v);
                        break;
                    case R.id.btn_build_buy://建筑购买
                        MainActivity._this.onBuildBuyClick(v);
                        break;
                    case R.id.btn_increament_confirm: //增值确认
                        MainActivity._this.onIncrementCloseClick(v);
                        break;
                    case R.id.btn_friend_last_page://好友上一页
                        MainActivity._this.onMyFriendListPageLast(v);
                        break;
                    case R.id.btn_friend_next_page://好友下一页
                        MainActivity._this.onMyFriendListPageNext(v);
                        break;
                    case R.id.btn_friend_active_open://打开好友激活
                        MainActivity._this.onMyFriendActiveClick(v);
                        break;
                    case R.id.btn_friend_search:
                        MainActivity._this.onFriendSearchClick(v); //好友搜索
                        break;
                    case R.id.btn_friend_active_submit:
                        MainActivity._this.onFriendActiveBtnClick(v);//好友激活
                        break;
                    case R.id.btn_invite_confirm:
                        MainActivity._this.inviteFriend(v);
                        break;
                    case R.id.tab_record:
                    case R.id.tab_news:
                    case R.id.tab_report:
                        MainActivity._this.onDealTabClick(v);
                        break;
                    case R.id.btn_deal_page_next:
                        MainActivity._this.onDealNextPageCLick(v);//交易信息下一页
                        break;
                    case R.id.btn_del_page_last:
                        MainActivity._this.onDealLastPageCLick(v);//交易信息上一页
                        break;
                    case R.id.btn_deal_open:
                        MainActivity._this.jumpToDealSearch(v);//打开交易弹窗
                        break;
                    case R.id.btn_deal_send:
                        MainActivity._this.onDealSendTranClick(v);//最终确定交易
                        break;
                    case R.id.btn_deal_search:
                        MainActivity._this.onDealSearchClick(v);//交易搜索
                        break;
                    case R.id.btn_deal_submit:
                        MainActivity._this.onDealBtnClick(v); //交易发送短信
                        break;

                }
            }
        });
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                //在按下事件中设置滤镜
                setFilter();
                break;
            case MotionEvent.ACTION_UP:
                //由于捕获了Touch事件，需要手动触发Click事件

                performClick();
            case MotionEvent.ACTION_CANCEL:
                //在CANCEL和UP事件中清除滤镜
                removeFilter();
                break;
            default:
                break;
        }
        return true;
    }

    /**
     *   设置滤镜
     */
    private void setFilter() {
        //先获取设置的src图片
        Drawable drawable=getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable==null) {
            drawable=getBackground();
        }
        if(drawable!=null){
            //设置滤镜
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);;
        }
    }
    /**
     *   清除滤镜
     */
    private void removeFilter() {
        //先获取设置的src图片
        Drawable drawable=getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable==null) {
            drawable=getBackground();
        }
        if(drawable!=null){
            //清除滤镜
            drawable.clearColorFilter();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }


}
